/* 
** (Copyright) sejishikong <sejishikong@263.net>
** Authors:    sejishikong <sejishikong@263.net>
**             Wei Lian <lianwei2003.student@sina.com> 
** Maintainer: Wei Lian
**    This program is free software; you can redistribute it and/or modify
**    it under the terms of the GNU General Public License Version 2 as
**    published by the Free Software Foundation;
**
**    This program is distributed in the hope that it will be useful,
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**    GNU General Public License for more details.
**
**    You should have received a copy of the GNU General Public License
**    along with this program; if not, write to the Free Software
**    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
*/

#include"sopcast.h"
#include "mylistitem.h"
#include "config.h"
#include <qdom.h>

/*static const char *folder_closed_xpm[] = {
  "16 16 9 1",
  "g c #808080",
  "b c #c0c000",
  "e c #c0c0c0",
  "# c #000000",
  "c c #ffff00",
  ". c None",
  "a c #585858",
  "f c #a0a0a4",
  "d c #ffffff",
  "..###...........",
  ".#abc##.........",
  ".#daabc#####....",
  ".#ddeaabbccc#...",
  ".#dedeeabbbba...",
  ".#edeeeeaaaab#..",
  ".#deeeeeeefe#ba.",
  ".#eeeeeeefef#ba.",
  ".#eeeeeefeff#ba.",
  ".#eeeeefefff#ba.",
  ".##geefeffff#ba.",
  "...##gefffff#ba.",
  ".....##fffff#ba.",
  ".......##fff#b##",
  ".........##f#b##",
  "...........####."
};

static const char *folder_open_xpm[] = {
  "16 16 11 1",
  "# c #000000",
  "g c #c0c0c0",
  "e c #303030",
  "a c #ffa858",
  "b c #808080",
  "d c #a0a0a4",
  "f c #585858",
  "c c #ffdca8",
  "h c #dcdcdc",
  "i c #ffffff",
  ". c None",
  "....###.........",
  "....#ab##.......",
  "....#acab####...",
  "###.#acccccca#..",
  "#ddefaaaccccca#.",
  "#bdddbaaaacccab#",
  ".eddddbbaaaacab#",
  ".#bddggdbbaaaab#",
  "..edgdggggbbaab#",
  "..#bgggghghdaab#",
  "...ebhggghicfab#",
  "....#edhhiiidab#",
  "......#egiiicfb#",
  "........#egiibb#",
  "..........#egib#",
  "............#ee#"
};

static const char *folder_locked[] = {
  "16 16 10 1",
  "h c #808080",
  "b c #ffa858",
  "f c #c0c0c0",
  "e c #c05800",
  "# c #000000",
  "c c #ffdca8",
  ". c None",
  "a c #585858",
  "g c #a0a0a4",
  "d c #ffffff",
  "..#a#...........",
  ".#abc####.......",
  ".#daa#eee#......",
  ".#ddf#e##b#.....",
  ".#dfd#e#bcb##...",
  ".#fdccc#daaab#..",
  ".#dfbbbccgfg#ba.",
  ".#ffb#ebbfgg#ba.",
  ".#ffbbe#bggg#ba.",
  ".#fffbbebggg#ba.",
  ".##hf#ebbggg#ba.",
  "...###e#gggg#ba.",
  ".....#e#gggg#ba.",
  "......###ggg#b##",
  ".........##g#b##",
  "...........####."
};

static const char *pix_file[] = {
  "16 16 7 1",
  "# c #000000",
  "b c #ffffff",
  "e c #000000",
  "d c #404000",
  "c c #c0c000",
  "a c #ffffc0",
  ". c None",
  "................",
  ".........#......",
  "......#.#a##....",
  ".....#b#bbba##..",
  "....#b#bbbabbb#.",
  "...#b#bba##bb#..",
  "..#b#abb#bb##...",
  ".#a#aab#bbbab##.",
  "#a#aaa#bcbbbbbb#",
  "#ccdc#bcbbcbbb#.",
  ".##c#bcbbcabb#..",
  "...#acbacbbbe...",
  "..#aaaacaba#....",
  "...##aaaaa#.....",
  ".....##aa#......",
  ".......##......."
};*/


void
Sopcast::fetchchannel ()
{
  if (fetchingchannel == FALSE)
    {
      fetchingchannel = TRUE;
      buttonchannel->setOn (TRUE);
      if (!bufchannel->open (IO_ReadWrite))
	{
	  printf ("cannot create the buffer\n");
	  return;
	}

      QString str = vboxconfig->editchannelurl->text ();
      http->setHost (str.section ("/", 2, 2));	//"www.sopcast.org");
      http->get ("/" + str.section ("/", 3), bufchannel);	//"/channel/index.php", bufchannel);
    }
  else
    {
      if (!buttonchannel->isOn ())
	buttonchannel->setOn (TRUE);
    }
}



void
Sopcast::httpdone (bool error)
{
  ///reset button state
  if (buttonchannel->isOn ())
    buttonchannel->setOn (FALSE);
  fetchingchannel = FALSE;

  if (error)
    {
      fprintf (stderr, "load page error!\n");
      return;
    }

  ///disable sorting
  if (!vboxconfig->radiochannelsort->isOn ())
    listchannel->setSortColumn (-1);
  else
    listchannel->setSortColumn (0);
  ///

  bufchannel->reset ();
  QDomDocument doc ("mydocument");
  doc.setContent (bufchannel);
  bufchannel->close ();

  QDomElement root = doc.documentElement ();
  QDomNode node = root.firstChild ();

  QDomNode channelnode, namenode;
  MyListItem *groupitem, *item;

  updateopenstate ();
  listchannel->clear ();
  ///group
  while (!node.isNull ())
    {
      if (node.isElement () && node.nodeName () == "group")
	{
	  groupitem = new MyListItem (listchannel);
	  groupitem->setText (0, node.firstChild ().toText ().nodeValue ());
	  ///channel
	  int count = 0;
	  channelnode = node.toElement ().firstChild ();
	  while (!channelnode.isNull ())
	    {
	      if (channelnode.isElement ()
		  && channelnode.nodeName () == "channel")
		{
		  ///element
		  item = new MyListItem (groupitem);
		  int i = 0;
		  namenode = channelnode.toElement ().firstChild ();
		  while (!namenode.isNull ())
		    {
		      if (namenode.nodeName () != "sop_address")
			{
			  item->setText (i,
					 namenode.firstChild ().toText ().
					 nodeValue ());
			}
		      else
			{
			  item->setText (i,
					 namenode.firstChild ().
					 toElement ().firstChild ().
					 toText ().nodeValue ());

			}
		      i++;
		      namenode = namenode.nextSibling ();
		    }
		  ///hide null channel
		  if (item->text (9) == "null")
		    {
		      item->setEnabled (FALSE);
		      if (!vboxconfig->radionullchannel->isOn ())
			item->setVisible (FALSE);
//                            item->setPixmap (0, folder_closed_xpm);
		    }
		  ///filting out channel
		  else if (channelnode.toElement ().attribute ("type").
			   toInt () == 1)
		    {
		      item->setEnabled (FALSE);
//                    if (!vboxconfig->radionullchannel->isOn ())
		      item->setVisible (FALSE);
		    }
		  else
		    {
		      count++;
//                          item->setPixmap (0, folder_open_xpm);
		    }

		}
	      channelnode = channelnode.nextSibling ();
	    }

	  ///hide null group
	  if (count == 0)
	    {
	      groupitem->setEnabled (FALSE);
	      if (!vboxconfig->radionullchannel->isOn ())
		groupitem->setVisible (FALSE);
	    }
	}
      node = node.nextSibling ();
    }
  ///set open
  QListViewItem *myChild = listchannel->firstChild ();
  int count = 0;
  while (myChild)
    {
      if (stateopen[count] == 1)
	myChild->setOpen (TRUE);
      myChild = myChild->nextSibling ();
      count++;
    }

}
